﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.Entity.Infrastructure;

namespace EFDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //SaveData();
            //QueryData1();
            QueryData2();
            //AddData1();
            //AddData2();
            //UpdateData1();
            //UpdateData2();
            //DeleteData1();
            //DeleteData2();

            Console.WriteLine("OK");
            Console.ReadKey();
        }

        static void SaveData()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Dept dept = new Dept
                {
                    DeptName = "信息部",
                    DeptCode = "IT"
                };

                Emp e1 = new Emp
                {
                    EmpName = "hyc",
                    EmpCode = "19130606",
                    Age = 18,
                    Gender = "男",
                    Dept = dept,
                    InDate = DateTime.Now,
                };

                Emp e2 = new Emp
                {
                    EmpName = "zbj",
                    EmpCode = "18130828",
                    Age = 19,
                    Gender = "女",
                    Dept = dept,
                    InDate = DateTime.Now,
                };

                dept.Emps.Add(e1);
                dept.Emps.Add(e2);

                context.Emp.AddRange(new List<Emp> { e1, e2 });
                context.Dept.Add(dept);

                context.SaveChanges();
            }
        }

        static void QueryData1()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Emp emp = context.Emp.Select(x => x).First();
                Console.WriteLine(emp.Dept?.DeptName);

                Dept dept = context.Dept.Select(x => x).First();
                Console.WriteLine(dept.DeptName);
                foreach (var item in dept.Emps)
                {
                    Console.WriteLine(item.EmpName);
                }
            }
        }

        static void QueryData2()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                string sql = "select * from Emp where EmpId=@EmpId";
                SqlParameter parameter = new SqlParameter("@EmpId", 1);
                DbRawSqlQuery<Emp> emps = context.Database.SqlQuery<Emp>(sql, parameter);
                foreach (Emp item in emps)
                {
                    Console.WriteLine(item.EmpName);
                }
            }
        }

        static void AddData1()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Dept dept = context.Dept.First();
                Emp e = new Emp
                {
                    EmpName = "cxh",
                    EmpCode = "20230814",
                    Age = 30,
                    Gender = "女",
                    Dept = dept,
                    InDate = DateTime.Now,
                };
                context.Emp.Add(e);
                context.SaveChanges();
            }
        }

        static void AddData2()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Dept dept = context.Dept.First();
                Emp e = new Emp
                {
                    EmpName = "cxf",
                    EmpCode = "20220810",
                    Age = 28,
                    Gender = "女",
                    Dept = dept,
                    InDate = DateTime.Now,
                };
                context.Entry(e).State = EntityState.Added;
                context.SaveChanges();
            }
        }

        static void UpdateData1()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Emp emp = context.Emp.Find(1);
                emp.Age = 30;
                context.SaveChanges();
            }
        }

        static void UpdateData2()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Emp emp = new Emp
                {
                    EmpId = 2,
                    EmpName = "zbj2",
                    EmpCode = "18130828",
                    Gender = "女",
                    DeptId = 1
                };

                context.Emp.Attach(emp);//不加此行也能实现需求

                emp.Age = 24;
                emp.InDate = DateTime.Now;
                context.Entry(emp).State = EntityState.Modified;
                context.SaveChanges();
            }
        }

        static void DeleteData1()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Emp emp = context.Emp.Find(4);
                context.Emp.Remove(emp);
                context.SaveChanges();
            }
        }

        static void DeleteData2()
        {
            using (EFDemoContext context = new EFDemoContext())
            {
                Emp emp = new Emp
                {
                    EmpId = 6
                };

                context.Emp.Attach(emp);//不加此行也能实现需求

                context.Entry(emp).State = EntityState.Deleted;
                context.SaveChanges();
            }
        }
    }
}
